package com.test.controller;


import com.test.service.StuOperatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ClassUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.Date;

import static com.test.utils.ErCodeUtil.generateQRCode;
import static com.test.utils.ErCodeUtil.parseQRCode;

@RestController
@RequestMapping("/acountQRCode")
public class AcountQRCodeController {

    @Autowired
    private StuOperatService stuOperatService;

    @RequestMapping(value="/codePicture")
    @ResponseBody
    public String codePicture(HttpServletRequest request, String codeName
            , String codeText, HttpSession session){
        String acount  = session.getAttribute("stu_u_acount").toString();
        String password = stuOperatService.getUpassword(acount);
        codeText = acount+","+password;
        int width = 200; // 二维码图片的宽
        int height = 200; // 二维码图片的高
        String format = "png"; // 二维码图片的格式
        try {
            // 生成二维码图片，并返回图片路径
//            String realpath = "G:/erCodeImage/";

            String realpath = ResourceUtils.getURL("").getPath();
            String contextPath = realpath+"src/main/resources/static/images/erCode/"+acount+".png";
            File tmpFile = new File(contextPath);
            if (!tmpFile.exists()) {
                tmpFile.mkdirs();
            }
            String pathName = generateQRCode(codeText, width, height, format,contextPath);
            System.out.println("生成二维码的图片路径： " + pathName);

            String content = parseQRCode(pathName);
            System.out.println("解析出二维码的图片的内容为： " + content);
            String arr[] = pathName.split("static");
            String pathNameStr = arr[1];
            try{
                stuOperatService.insertCodePath(acount,pathNameStr);
            }catch (Exception e){
            }
            return pathNameStr;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }
}
